package com.example.bindservice;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;
    private Button button_start;
    private Button button_pause;
    private Button button_stop;
    private Button button_exit;
    MyConn conn=new MyConn();
    MyService myService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Intent intent =new Intent(this,MyService.class);
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        button_start = (Button) findViewById(R.id.button_start);
        button_pause = (Button) findViewById(R.id.button_pause);
        button_stop = (Button) findViewById(R.id.button_stop);
        button_exit = (Button) findViewById(R.id.button_exit);

        button_start.setOnClickListener(this);
        button_pause.setOnClickListener(this);
        button_stop.setOnClickListener(this);
        button_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        button_start.setBackgroundColor(getResources().getColor(R.color.greenyellow));
        button_pause.setBackgroundColor(getResources().getColor(R.color.greenyellow));
        button_stop.setBackgroundColor(getResources().getColor(R.color.greenyellow));
        button_exit.setBackgroundColor(getResources().getColor(R.color.greenyellow));
        button_start.setEnabled(true);
        button_pause.setEnabled(true);
        button_stop.setEnabled(true);
        switch (v.getId()) {
            case R.id.button_start:
                button_start.setBackgroundColor(getResources().getColor(R.color.darkseagreen));
                myService.play();
                button_start.setEnabled(false);
                break;
            case R.id.button_pause:
                button_pause.setBackgroundColor(getResources().getColor(R.color.darkseagreen));
                myService.pause();
                button_pause.setEnabled(false);
                break;
            case R.id.button_stop:
                button_stop.setBackgroundColor(getResources().getColor(R.color.darkseagreen));
                myService.stop();
                button_pause.setEnabled(false);
                button_stop.setEnabled(false);
                break;
            case R.id.button_exit:
                button_exit.setBackgroundColor(getResources().getColor(R.color.darkseagreen));
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获取 MusicBinder 的实例
            MyService.MyBinder binder=(MyService.MyBinder) service;
            myService=binder.getService();//返回一个 MusicService 对象
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
